package edu.ccut.saturn.studio.wfg.wizard;

import java.io.File;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnBundleUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnEndNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnInformationNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnName;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnStartNode;

public class WfgWizardsPage extends SaturnWizardPage {

	Shell shell;
	private static final String START_NODE_VALUE = Messages.getString("WfgWizardsPage.start"); //$NON-NLS-1$
	private static final String END_NODE_VALUE = Messages.getString("WfgWizardsPage.end"); //$NON-NLS-1$
	private IProject project;

	public static final String Wfg_WIZARD_FILE_NAME = Messages.getString("WfgWizardsPage.0"); //$NON-NLS-1$

	private static final String INFO = Messages.getString("WfgWizardsPage.1"); //$NON-NLS-1$
	private static final String START = Messages.getString("WfgWizardsPage.2"); //$NON-NLS-1$
	private static final String END = Messages.getString("WfgWizardsPage.3"); //$NON-NLS-1$

	private Text textName;
	private Text textKey;
	private Text textVer;
	private Text textAuthor;
	private Text textDec;

	public WfgWizardsPage() {
		super();
		setTitle(Messages.getString("WfgWizardsPage.ComponentInformation")); //$NON-NLS-1$
	}

	private SaturnInformationNode iniInfoNode() {
		SaturnInformationNode infoNode = new SaturnInformationNode();
		infoNode.setDescription(textDec.getText().trim());
		infoNode.setKey(textKey.getText().trim());
		infoNode.setName(textName.getText().trim());
		infoNode.setVersion(textVer.getText().trim());
		infoNode.setAuthor(textAuthor.getText().trim());
		return infoNode;
	}

	private SaturnStartNode iniStartNode() {
		SaturnStartNode saturnStartNode = new SaturnStartNode();
		saturnStartNode.setName(START_NODE_VALUE);
		saturnStartNode.setStartPolicy(Messages.getString("WfgWizardsPage.4")); //$NON-NLS-1$
		saturnStartNode.setTimeLimit(Messages.getString("WfgWizardsPage.5")); //$NON-NLS-1$
		saturnStartNode.setOffsetMode(Messages.getString("WfgWizardsPage.6")); //$NON-NLS-1$
		saturnStartNode.setDescription(Messages.getString("WfgWizardsPage.7")); //$NON-NLS-1$
		return saturnStartNode;
	}

	private SaturnEndNode iniEndNode() {
		SaturnEndNode saturnEndNode = new SaturnEndNode();
		saturnEndNode.setName(END_NODE_VALUE);
		saturnEndNode.setPolymeMode(Messages.getString("WfgWizardsPage.8")); //$NON-NLS-1$
		saturnEndNode.setDescription(Messages.getString("WfgWizardsPage.9")); //$NON-NLS-1$
		return saturnEndNode;
	}

	@Override
	public void initalValues(Map<String, Object> values) {
		SaturnInformationNode infoNode = iniInfoNode();
		SaturnStartNode saturnStartNode = iniStartNode();
		SaturnEndNode saturnEndNode = iniEndNode();
		values.put(Wfg_WIZARD_FILE_NAME, this.textName.getText().trim());
		values.put(INFO, infoNode);
		values.put(START, saturnStartNode);
		values.put(END, saturnEndNode);
	}

	public void createControl(Composite parent) {

		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		setControl(container);

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages.getString("WfgWizardsPage.Name"));  //$NON-NLS-1$

		textName = new Text(container, SWT.BORDER);
		textName.setTextLimit(50);
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				textKey.setText(getBundleName() + SaturnName.TWO_COLONS
//						+ SaturnName.SLANT_LINE
						+ getComponentKey(textName.getText() + Messages.getString("WfgWizardsPage.10"))); //$NON-NLS-1$
				validate();
			}
		});
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textName.setLayoutData(gd_textName);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText(Messages.getString("WfgWizardsPage.13"));  //$NON-NLS-1$

		textKey = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_textKey = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textKey.setLayoutData(gd_textKey);
		textKey.setText(getBundleName() + SaturnName.TWO_COLONS
				+ getComponentKey(Messages.getString("WfgWizardsPage.14"))); //$NON-NLS-1$

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setText(Messages.getString("WfgWizardsPage.15"));  //$NON-NLS-1$

		textVer = new Text(container, SWT.BORDER);
		textVer.setTextLimit(50);
		textVer.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_textVer = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textVer.setLayoutData(gd_textVer);

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setText(Messages.getString("WfgWizardsPage.16"));   //$NON-NLS-1$

		textAuthor = new Text(container, SWT.BORDER);
		textAuthor.setTextLimit(50);
		textAuthor.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_textAuthor = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textAuthor.setLayoutData(gd_textAuthor);

		final Label label_4 = new Label(container, SWT.NONE);
		label_4.setText(Messages.getString("WfgWizardsPage.12"));  //$NON-NLS-1$

		textDec = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		textDec.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_textDec.heightHint = 69;
		textDec.setLayoutData(gd_textDec);
		validate();
	}

	public boolean validate() {
		String fileName = textName.getText().trim();
		String version = textVer.getText().trim();
		String author = textAuthor.getText().trim();
		String description = textDec.getText().trim();

		setPageComplete(false);

		if (new File(getFullPath() + Messages.getString("WfgWizardsPage.17") + fileName + Messages.getString("WfgWizardsPage.18")).exists()) { //$NON-NLS-1$ //$NON-NLS-2$
			setErrorMessage(Messages.getString("WfgWizardsPage.19"));  //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(fileName)) {
			setErrorMessage(Messages.getString("WfgWizardsPage.20"));  //$NON-NLS-1$
			return false;
		}

		if (!SaturnValidation.isValidProgramStr(fileName)) {
			setErrorMessage(Messages.getString("WfgWizardsPage.NameLimit")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(version)) {
			setErrorMessage(Messages.getString("WfgWizardsPage.22"));  //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(version)) {
			setErrorMessage(Messages.getString("WfgWizardsPage.23"));  //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(author)) {
			setErrorMessage(Messages.getString("WfgWizardsPage.24"));  //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(author)) {
			setErrorMessage(Messages.getString("WfgWizardsPage.25"));  //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(description)) {
			setDescription(Messages.getString("WfgWizardsPage.26"));  //$NON-NLS-1$
		}

		setErrorMessage(null);
		setPageComplete(true);
		return true;
	}

	/**
	 * Called by the wizard to initialize the receiver's cached selection.
	 * 
	 * @param selection
	 *            the selection or <code>null</code> if none
	 */
	public void init(ISelection selection) {
	}

	private String getBundleName() {
		String bundleName = null;
		if (project == null) {
			this.project = SaturnProjectUtil.getProject(super.getSelection());
		}
		try {
			bundleName = SaturnBundleUtil.getBundleSymbolicName(project);

		} catch (Exception e) {

			e.printStackTrace();
		}
		return bundleName;
	}

}
